#include "stdafx.h"
#include "VisualStudioATLApp.h"
#include "MainCommand.h"
#include "VisualStudioATLView.h"
#include "VisualStudioATL_i.c"



// CVisualStudioATLApp construction

CVisualStudioATLApp::CVisualStudioATLApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

// The one and only CVisualStudioATLApp object
CVisualStudioATLApp theApp;

// CVisualStudioATLApp initialization
 //ART_USER_MFC
int CVisualStudioATLApp::WinMain(int nShowCmd)
{
	HRESULT hr = E_FAIL;

	SAF_TRY;

	hr = safBaseClass::InitComFramework(safBaseClass::NotLoadLanguage);

	if (S_OK != hr)
		throw saf::CComException(hr);

	hr = this->DispEventAdvise((IUnknown*)this->GetComFramework());
	if (S_OK != hr)
		throw saf::CComException(hr);

	hr = CreateMDISplitterWnd(SW_SHOW);
	if (S_OK != hr)
		throw saf::CComException(hr);
	hr = GetComFramework()->Run();

//	hr = this->DispEventUnadvise((IUnknown*)this->GetComFramework());
//	if (S_OK != hr)
//		throw saf::CComException(hr);

	SAF_CATCH;

	::CoUninitialize();

	return TRUE;
}


//Application quit.
void __stdcall CVisualStudioATLApp::OnQuit(VARIANT_BOOL* bExit)
{
	this->DispEventUnadvise((IUnknown*)this->GetComFramework());
}


void __stdcall CVisualStudioATLApp::OnCloseMainFrame(BSTR strKind, saf::IMainFrame* pMainFrame, VARIANT_BOOL *bExit)
{
	CComBSTR	strTitle;

	strTitle.LoadString(IDR_MAINFRAME);

	if (StrCmpI(strTitle, strKind) == 0)
	{
		this->m_pMainFrame.Release();
	}
}


void __stdcall CVisualStudioATLApp::OnPopupMenu(BSTR strKind, saf::IPopupMenuBar* pPopupMenuBar, IDispatch* pKindObject)
{
}


HRESULT CVisualStudioATLApp::CreateMDISplitterWnd(int nShowCmd)
{
	CComBSTR	strTitle;
	HRESULT		hr		= S_FALSE;
	HICON		hIcon	= (HICON)::LoadImage(SafGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME),
												IMAGE_ICON,	16,	16,	LR_DEFAULTCOLOR);
	HMENU		hMenu	= ::LoadMenu(SafGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));

	strTitle.LoadString(IDR_MAINFRAME);
	hr = this->GetComFramework()->CreateMainFrame(saf::safMainFrameTypeMDISplitter, 
													hMenu, (LONG_PTR)hIcon, strTitle, &m_pMainFrame);
	if (S_OK == hr)
	{
		CMainCommand	*pMainCommand = new CMainCommand();
		this->m_pMainFrame->put_NotifyCommand((saf::INotifyCommand*)pMainCommand);  
		this->GetComFramework()->put_ApplicationName(strTitle);

		this->CreateTools();
		this->CreateStatusBar();

		CComPtr<saf::ISplitterWnd>	pSplitterWnd;
		CComPtr<saf::ISplitterCell>	pLeftCell;
		CComPtr<saf::ISplitterCell>	pRightCell;
		CComPtr<saf::IGenericPanel>	pLeftGenericPanel;
		CComPtr<saf::IGenericPanel>	pRightGenericPanel;

		this->m_pMainFrame->get_SplitterWnd(&pSplitterWnd);
		pSplitterWnd->CreateSplitterItem(saf::safSplitterWndTypeVertical, 250,
			&pRightCell, &pRightCell, &pLeftGenericPanel, &pRightGenericPanel);

		// Left Cell.
		CVisualStudioATLView *pLeftView = NULL;
		pLeftView = new CVisualStudioATLView();
		pLeftGenericPanel->InsertItem(0, L"LeftCell", NULL, (saf::IDataView*)pLeftView, (LONG_PTR*)pLeftView, NULL);

		//Move IMDIContainer to right cell.
		CComPtr<saf::IMdiContainer>		pMDIContainer;
		m_pMainFrame->get_MdiContainer(&pMDIContainer);
		CComPtr<saf::IDataView>	pDataView;
		pMDIContainer->QueryInterface(saf::IID_IDataView, (void**)&pDataView);
		pRightGenericPanel->InsertItem(0, L"MdiContainer", NULL, (saf::IDataView*)pDataView,  (LONG_PTR*)pDataView.p, NULL);

		//		pSplitterWnd->UpdateLayout();

		this->m_pMainFrame->ShowWindow(nShowCmd);
	}

	return hr;
}


void CVisualStudioATLApp::CreateTools()
{
	ATLASSERT(NULL != m_pMainFrame);

	CComPtr<saf::ICommandBars>	pCommandBars;

	this->m_pMainFrame->get_CommandBars(&pCommandBars);

	CComBSTR  strTitile;
	strTitile.LoadString(IDR_MAINFRAME);

	CComPtr<saf::ICommandBar>	pResCommandBar;
	pCommandBars->InsertResourceCommandBar(strTitile, IDR_MAINFRAME,
											(LONG_PTR)SafGetInstanceHandle(), 1, &pResCommandBar);

	CComPtr<saf::IMenuBar>	pMenuBar;
	pCommandBars->get_MenuBar(&pMenuBar);
	pMenuBar->AddImageList((LONG_PTR)SafGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), NULL);
}

void CVisualStudioATLApp::CreateStatusBar()
{
	CComQIPtr<saf::IStatusBar>	_pStatusBar;

	saf::safStatusBarItemInfo	_safItemInfo[] = {
		{ID_FILE_CLOSE, 200},
		{ID_FILE_NEW, 100}
	};

	m_pMainFrame->get_StatusBar(&_pStatusBar);
	_pStatusBar->SetPanes(_safItemInfo, 2);
}

